function [f] = f_density_lognormal(a,q_bar, muq, sigq, sig, q_up)
%LOGLIKELIHOOD
% inputs:  b,as(asset),muzT,sigq,sig,cs,c0,c1,tauT,kT
% outputs: logLikelihood
%   see the model file


if false
    a = 16;
    muq = 3
    sigq = 0.7
    sig = 0.02
    tau = 0.01
    k = 0.0004
    q_up = log(10.85);
    beta = 87
    a = apre
    b = apost
    
    q_bar = log(10);
   
    
end

Phi  = @(z) normcdf(z);
phi  = @(z) normpdf(z);
e    = exp(1);

% Making sure to have column vectors
a = a(:);

 
if q_bar == q_up
    szr  = sigq.^2./(sig.^2 + sigq.^2);         % sigq's relative size
    sla  = (a   - muq)./sigq;     % standardized log a*e/b
    f = (1./(sigq)).*sqrt(.5.*szr./pi).* exp(-.5.*szr.*sla.^2);
else 
    % define f, too long -> define subcomponent and then f
    slbar  = (q_bar- muq)./sigq;     % standardized log Abar*e/b
    slup   = (q_up      - muq)./sigq;     % standardized log z upper bar
    sla    = (a   - muq)./sigq;     % standardized log a*e/b

    szr  = sigq.^2./(sig.^2 + sigq.^2);         % sigq's relative size
    ssz  = (sig.*sigq)./(sig.^2+sigq.^2);         % sig sigq

    f =   (1./(sig)).*phi((1./sig).*(a-q_bar)) .* ( Phi(slup) - Phi(slbar))  ...
        + (1./(sigq)).*sqrt(.5.*szr./pi).* exp(-.5.*szr.*sla.^2).*( 1-Phi(     ...
                   ((1./sig).*(a-q_bar)-ssz.*sla)./sqrt(szr) ))              ...
        + (1./(sigq)).*sqrt(.5.*szr./pi).* exp(-.5.*szr.*sla.^2) .* Phi(        ...
          ( (1./sig).*(a- q_up) - ssz.*sla)./sqrt(szr) );
end


f(f==0) = 1e-100;

end

